//
//  detailsViewController.m
//  lmq
//
//  Created by 千翼网络科技 on 2017/1/18.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "detailsViewController.h"
#import "detailsFirstTableViewCell.h"
#import "detailsSecondTableViewCell.h"
#import "cartViewController.h"
#import "AppDelegate.h"
#import "detailsShareViewController.h"
#import "configddd.h"
#import "WXApi.h"
#import <MJRefresh/MJRefresh.h>
#import "UIImageView+WebCache.h"
#import "loginViewController.h"
#import "MBProgressHUD.h"

@interface detailsViewController ()

@end

@implementation detailsViewController

- (void)viewWillAppear:(BOOL)animated{
    
    if ([PublicFunction getConfigWithKey:@"uid"]) {
        
        detailsTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopic)];
        [detailsTableView.mj_footer endRefreshing];
        
        [self requestData];
    }
    else{
        
        loginViewController *loginVc =[[loginViewController alloc]init];
//        [self.navigationController presentViewController:loginVc animated:YES completion:^{}];
        [self.navigationController pushViewController:loginVc animated:YES];
        
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitle:@"商品详情"];
    
    tapGesure = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelBtnClick)];
    
    footViewHeight = screenScale(66);
    
    
    
    self.shadow = [[UIView alloc]init];
    
    rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, screenScale(35), screenScale(35))];
    rightButton.selected = NO;
    [rightButton addTarget:self action:@selector(rightNavItemClick) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"share"] forState:UIControlStateSelected];
    UIBarButtonItem *rightBarButton=[[UIBarButtonItem alloc]initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem=rightBarButton;
    
    UIView *xView = [[UIView alloc]initWithFrame:CGRectMake(0, DEF_SCREEN_HEIGHT-screenScale(81)-64, DEF_SCREEN_WIDTH, screenScale(1))];
    xView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:xView];
    
//底部view
    
    //加入购物袋
    UIView *bottomView=[[UIView alloc]initWithFrame:CGRectMake(0, DEF_SCREEN_HEIGHT-64-screenScale(80), DEF_SCREEN_WIDTH, screenScale(80))];
    [self.view addSubview:bottomView];
    
    UIButton *gwdButton=[[UIButton alloc]initWithFrame:CGRectMake(DEF_SCREEN_WIDTH/2, 0, DEF_SCREEN_WIDTH / 2, screenScale(80))];
    gwdButton.titleLabel.font = get4sFont(14);
    [gwdButton setTitle:@"加入购物袋" forState:UIControlStateNormal];
    [gwdButton addTarget:self action:@selector(gwdButtonClick) forControlEvents:UIControlEventTouchUpInside];
    gwdButton.backgroundColor=[ZJCModel colorWithString:@"222222"];
    [bottomView addSubview:gwdButton];
    
    //购物袋
    UIButton *hButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, scrWidth()/2/2, screenScale(80))];
    [hButton addTarget:self action:@selector(hButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:hButton];
    [hButton bringSubviewToFront:self.view];
    
    UIImageView *gImageView=[[UIImageView alloc]initWithFrame:CGRectMake(screenScale(20), screenScale(24), screenScale(33), screenScale(33))];
    gImageView.image=[UIImage imageNamed:@"shopping_red"];
    
    [hButton addSubview:gImageView];
    
    UILabel *gLabel=[[UILabel alloc]initWithFrame:CGRectMake(screenScale(70), 0, screenScale(100), screenScale(80))];
    gLabel.text=@"购物袋";
    gLabel.font = get4sFont(13);
    [hButton addSubview:gLabel];
    
    //收藏
    UIButton *xButton=[[UIButton alloc]initWithFrame:CGRectMake(DEF_SCREEN_WIDTH/2/2 +screenScale(1), 0, DEF_SCREEN_WIDTH/2/2 + screenScale(1), screenScale(80))];
    [xButton addTarget:self action:@selector(xButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    xImageView = [[UIImageView alloc]initWithFrame:CGRectMake(screenScale(30), screenScale(27), screenScale(33), screenScale(33))];
    xImageView.image=[UIImage imageNamed:@"collection_nor"];
    [xButton addSubview:xImageView];
    
    UILabel *xLabel=[[UILabel alloc]initWithFrame:CGRectMake(screenScale(70), 0, screenScale(70), screenScale(80))];
    xLabel.text=@"收藏";
    xLabel.font = get4sFont(13);
    [xButton addSubview:xLabel];
    [bottomView addSubview:xButton];
    
    UIView *xiView=[[UIView alloc]initWithFrame:CGRectMake((DEF_SCREEN_WIDTH-screenScale(317))/2, screenScale(10), screenScale(1), screenScale(80 - 20))];
    xiView.backgroundColor=[ZJCModel colorWithString:@"cccccc"];
    [bottomView addSubview:xiView
     ];
    
    detailsTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-64-screenScale(82)) style:UITableViewStyleGrouped];
    detailsTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    detailsTableView.separatorStyle=UITableViewCellSelectionStyleNone;
    detailsTableView.backgroundColor=[ZJCModel colorWithString:@"f5f5f5"];
    detailsTableView.delegate=self;
    detailsTableView.dataSource=self;
    [self.view addSubview:detailsTableView];
}

- (void)requestData{

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    //[productParams setValue: self.myStr forKey:@"id"];//self.myStr
    [productParams setObject: self.myStr forKey:@"shop_id"];//self.myStr
    [productParams setObject: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    [productParams setObject: [PublicFunction getConfigWithKey:@"uid"] forKey:@"member_id"];
    [[MNetworkHandler sharedInstance]conURL:Category_Details
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       //                                       NSLog(@"/n/n/n/n/n===return===%@",returnData);
                                       
                                       if ([[returnData objectForKey:@"collection"] intValue] == 1) {
                                           
                                           xImageView.image=[UIImage imageNamed:@"collection_sel"];
                                       }else{
                                           
                                           xImageView.image=[UIImage imageNamed:@"collection_nor"];
                                       }
                                       detailsArr = [returnData objectForKey:@"data1"];
                                       detailsDict2 = [returnData objectForKey:@"data2"];
                                       detailsArr3 = [returnData objectForKey:@"shop_pic1"];
                                       picArr = [returnData objectForKey:@"shop_pic2"];
                                       [detailsTableView reloadData];
                                       
                                       [hud removeFromSuperview];
                                   }
                                   else{
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

#pragma mark ---------- 分享 ----------

- (void)setShadow:(UIView *)shadow{
    
    _shadow = shadow;
    _shadow.frame = CGRectMake(0, DEF_SCREEN_HEIGHT, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT);
    _shadow.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    _shadow.hidden = YES;
    [[[UIApplication sharedApplication]keyWindow]addSubview:_shadow];
    
    //  添加手势
    [_shadow addGestureRecognizer:tapGesure];
    
    //分享背景
    
    UIView *shareView = [[UIView alloc]initWithFrame:CGRectMake(0, screenScale(1136 - 278), DEF_SCREEN_WIDTH, screenScale(280))];
    shareView.backgroundColor = [UIColor whiteColor];
    [_shadow addSubview:shareView];
    
    //微信btn
    
    UIButton *wechatBtn = [[UIButton alloc]initWithFrame:CGRectMake(screenScale(180), screenScale(62), screenScale(84), screenScale(100))];
    [wechatBtn addTarget:self action:@selector(wechatBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [shareView addSubview:wechatBtn];
    
    UIImageView *wechatImg = [[UIImageView alloc]initWithFrame:CGRectMake(screenScale(8), 0, screenScale(68), screenScale(68))];
    wechatImg.image = [UIImage imageNamed:@"wechat"];
    [wechatBtn addSubview:wechatImg];
    
    UILabel *wechatLable = [[UILabel alloc]initWithFrame:CGRectMake(0, screenScale(75), screenScale(84), screenScale(30))];
    wechatLable.text = @"微信";
    wechatLable.textAlignment = NSTextAlignmentCenter;
    wechatLable.font = get4sFont(13);
    [wechatBtn addSubview:wechatLable];
    
    //朋友圈btn
    
    UIButton *friendsBtn = [[UIButton alloc]initWithFrame:CGRectMake(screenScale(380), screenScale(62), screenScale(84), screenScale(100))];
    [friendsBtn addTarget:self action:@selector(friendsBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [shareView addSubview:friendsBtn];
    
    UIImageView *friendsImg = [[UIImageView alloc]initWithFrame:CGRectMake(screenScale(8), 0, screenScale(68), screenScale(68))];
    friendsImg.image = [UIImage imageNamed:@"friends"];
    [friendsBtn addSubview:friendsImg];
    
    UILabel *friendLable = [[UILabel alloc]initWithFrame:CGRectMake(0, screenScale(75), screenScale(84), screenScale(30))];
    friendLable.text = @"朋友圈";
    friendLable.textAlignment = NSTextAlignmentCenter;
    friendLable.font = get4sFont(13);
    [friendsBtn addSubview:friendLable];
    
    //取消
    
    UIView *thread = [[UIView alloc]initWithFrame:CGRectMake(screenScale(20), screenScale(193), scrWidth() - screenScale(40), screenScale(1))];
    thread.backgroundColor = getColor(@"dcdcdc");
    [shareView addSubview:thread];
    
    UIButton *cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, screenScale(194), scrWidth(), screenScale(85))];
    cancelBtn.titleLabel.font = get4sFont(15);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [shareView addSubview:cancelBtn];
}

- (void)wechatBtnClick{
    
    NSLog(@"微信分享");
    
    if (![PublicFunction getConfigWithKey:@"token"]) {
        
        [PublicFunction showAlert:nil message:@"还没有登录呦" duration:5.0f];
        
        loginViewController *loginVc =[[loginViewController alloc]init];
        [self.navigationController presentViewController:loginVc animated:YES completion:^{}];
    }else{
        
        if (![PublicFunction getConfigWithKey:@"invitation"]) {
            
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
            hud.tag = 500;
            //提示文本
            hud.labelText = @"正在加载...";
            //是否有遮罩
            hud.dimBackground = YES;
            
            NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
            NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
            [productParams setValue:tokenStr forKey:@"token"];
            
            [[MNetworkHandler sharedInstance]conURL:My_QrCode
                                        networkType:NetWorkPOST
                                             params:productParams
                                           delegate:nil
                                            showHUD:NO
                                       successBlock:^(id returnData) {
                                           if ([[returnData objectForKey:@"code"]intValue]==1) {
                                               
                                               [hud removeFromSuperview];
                                               
                                               NSDictionary *dict1=[returnData objectForKey:@"data"];
                                               [PublicFunction saveConfig:dict1[@"invitation"] forKey:@"invitation"];
                                               
                                               WXMediaMessage *message = [WXMediaMessage message];
                                               message.title = @"乐麦圈";
                                               message.description = @"";
                                               [message setThumbImage:[UIImage imageNamed:@""]];
                                               
                                               WXWebpageObject *webpageObject = [WXWebpageObject object];
                                               webpageObject.webpageUrl = [@"http://lmq.quyum.cn/home/register/regweb/invitation/" stringByAppendingString:[PublicFunction getConfigWithKey:@"invitation"]];
                                               message.mediaObject = webpageObject;
                                               
                                               SendMessageToWXReq *req = [[SendMessageToWXReq alloc]init];
                                               req.bText = NO;
                                               req.message = message;
                                               req.scene = WXSceneSession;
                                               
                                               [WXApi sendReq:req];
                                               
                                           }
                                           else{
                                               
                                               [hud removeFromSuperview];
                                               [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                           }
                                       } failureBlock:^(NSError *error) {
                                           
                                           [hud removeFromSuperview];
                                           NSLog(@"%@",error);
                                           [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                                       }];
        }else{
        
            WXMediaMessage *message = [WXMediaMessage message];
            message.title = @"乐麦圈";
            message.description = @"";
            [message setThumbImage:[UIImage imageNamed:@""]];
            
            WXWebpageObject *webpageObject = [WXWebpageObject object];
            webpageObject.webpageUrl = [@"http://lmq.quyum.cn/home/register/regweb/invitation/" stringByAppendingString:[PublicFunction getConfigWithKey:@"invitation"]];
            message.mediaObject = webpageObject;
            
            SendMessageToWXReq *req = [[SendMessageToWXReq alloc]init];
            req.bText = NO;
            req.message = message;
            req.scene = WXSceneSession;
            
            [WXApi sendReq:req];
        }
    }
}

- (void)friendsBtnClick{
    
    NSLog(@"朋友圈分享");
    
    if (![PublicFunction getConfigWithKey:@"token"] ) {
        
        [PublicFunction showAlert:nil message:@"还没有登录呦" duration:5.0f];
        
        loginViewController *loginVc =[[loginViewController alloc]init];
        [self.navigationController presentViewController:loginVc animated:YES completion:^{}];
    }else{
        
        if (![PublicFunction getConfigWithKey:@"invitation"]) {
            
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
            hud.tag = 500;
            //提示文本
            hud.labelText = @"正在加载...";
            //是否有遮罩
            hud.dimBackground = YES;
            
            NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
            NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
            [productParams setValue:tokenStr forKey:@"token"];
            
            [[MNetworkHandler sharedInstance]conURL:My_QrCode
                                        networkType:NetWorkPOST
                                             params:productParams
                                           delegate:nil
                                            showHUD:NO
                                       successBlock:^(id returnData) {
                                           if ([[returnData objectForKey:@"code"]intValue]==1) {
                                               
                                               [hud removeFromSuperview];
                                               
                                               NSDictionary *dict1=[returnData objectForKey:@"data"];
                                               [PublicFunction saveConfig:dict1[@"invitation"] forKey:@"invitation"];
                                               
                                               WXMediaMessage *message = [WXMediaMessage message];
                                               message.title = @"乐麦圈";
                                               message.description = @"";
                                               [message setThumbImage:[UIImage imageNamed:@""]];
                                               
                                               WXWebpageObject *webpageObject = [WXWebpageObject object];
                                               //    webpageObject.webpageUrl = @"http://lmq.quyum.cn/home/register/regweb/invitation/a4bi31j1";
                                               webpageObject.webpageUrl = [@"http://lmq.quyum.cn/home/register/regweb/invitation/" stringByAppendingString:[PublicFunction getConfigWithKey:@"invitation"]];
                                               message.mediaObject = webpageObject;
                                               
                                               SendMessageToWXReq *req = [[SendMessageToWXReq alloc]init];
                                               req.bText = NO;
                                               req.message = message;
                                               req.scene = WXSceneTimeline;
                                               
                                               [WXApi sendReq:req];
                                               
                                           }
                                           else{
                                               
                                               [hud removeFromSuperview];
                                               [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                           }
                                       } failureBlock:^(NSError *error) {
                                           
                                           [hud removeFromSuperview];
                                           NSLog(@"%@",error);
                                           [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                                       }];
        }else{
        
            WXMediaMessage *message = [WXMediaMessage message];
            message.title = @"乐麦圈";
            message.description = @"";
            [message setThumbImage:[UIImage imageNamed:@""]];
            
            WXWebpageObject *webpageObject = [WXWebpageObject object];
            //    webpageObject.webpageUrl = @"http://lmq.quyum.cn/home/register/regweb/invitation/a4bi31j1";
            webpageObject.webpageUrl = [@"http://lmq.quyum.cn/home/register/regweb/invitation/" stringByAppendingString:[PublicFunction getConfigWithKey:@"invitation"]];
            message.mediaObject = webpageObject;
            
            SendMessageToWXReq *req = [[SendMessageToWXReq alloc]init];
            req.bText = NO;
            req.message = message;
            req.scene = WXSceneTimeline;
            
            [WXApi sendReq:req];
        }
    }
}

- (void)cancelBtnClick{
    
    NSLog(@"取消");
    
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    _shadow.frame = CGRectMake(0, DEF_SCREEN_HEIGHT, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT);
    _shadow.hidden = YES;
    [UIView commitAnimations];
}

#pragma mark - 数据源方法
#pragma mark 返回分组数
//指定有多少个分区(Section)，默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
#pragma mark 返回每组行数
//指定每个分区中有多少行，默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==1) {
        return detailsArr.count;
    }
    return 1;
}
#pragma mark - 代理方法
#pragma mark 设置每行高度（每行高度可以不一样）
//改变行的高度（实现这个代理方法后 rowHeight 设定的高度无效）
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==1) {
        
        return screenScale(41);
    }
    return screenScale(870);
}

//自定义section的头部
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section==1) {
        
        UIView *xView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, screenScale(75))];
        xView.backgroundColor=[UIColor whiteColor];
        
        UILabel *xLabel=[[UILabel alloc]initWithFrame:CGRectMake(screenScale(20), screenScale(26), screenScale(10), screenScale(28))];
        xLabel.backgroundColor=[ZJCModel colorWithString:@"222222"];
        [xView addSubview:xLabel];
        
        UILabel *sLable=[[UILabel alloc]initWithFrame:CGRectMake(screenScale(43), screenScale(26), screenScale(200), screenScale(28))];
        sLable.font = get4sFont(13);
        sLable.text=@"商品信息";
        [xView addSubview:sLable];
        return xView;
    }
    UIView *xView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, (0))];
    return xView;
}
//自定义section头部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   if (section==1){
       
        return screenScale(75);
    }
    return 0.01;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    if (section == 1) {
        
        footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), screenScale(66))];
        footerView.backgroundColor = getColor(@"f5f5f5");
        
        //刷新之后
        footerView2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), footViewHeight)];
        footerView2.hidden = !hidden;
        [footerView addSubview:footerView2];
        
        for (int i = 0; i < picArr.count; i ++) {
            
            int height = 0;
            
            for (int j = 0 ; j < i ; j ++) {
                
                height += [picHeightArray[j] intValue];
            }
            
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, height, scrWidth(), [picHeightArray[i] intValue])];
            [footerView2 addSubview:img];
            
            NSString *urlWare = [picArr[i] objectForKey:@"path"];
            NSString *urlN = manage(AP_HOST, urlWare);
            
            NSString *strUrl = [urlN stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
            NSURL *urlW = [NSURL URLWithString:strUrl];
            [img sd_setImageWithURL:urlW placeholderImage:[UIImage imageNamed:@"niunai"]];
        }
        
        //上拉刷新文字提示
        footerView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), screenScale(66))];
        footerView1.backgroundColor = getColor(@"f5f5f5");
        footerView1.hidden = hidden;
        [footerView addSubview:footerView1];
        
        UIView *threadView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), screenScale(1))];
        threadView.backgroundColor = getColor(@"dfdfdf");
        [footerView1 addSubview:threadView];
        
        UILabel *footLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), screenScale(66))];
        footLable.text = @"继续拖动，查看图文详情";
        footLable.font = get4sFont(11);
        footLable.textAlignment = NSTextAlignmentCenter;
        [footerView1 addSubview:footLable];
        
        return footerView;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    if (section == 1) {
        
        return footViewHeight;
    }
    return 0.01;
}

#pragma mark返回每行的单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section==1) {
        detailsSecondTableViewCell *cell=(detailsSecondTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"detailsSecondTableViewCell"];
        if(cell == nil){
            
            cell=[[detailsSecondTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"detailsSecondTableViewCell"];
        }
        if (detailsArr) {
            
            cell.pArr=detailsArr;
            NSDictionary *dict1=detailsArr[indexPath.row];
            NSString *str1=[dict1 objectForKey:@"key"];
            cell.lLabel.text=[NSString stringWithFormat:@"【%@】", str1];
            cell.rLabel.text=[dict1 objectForKey:@"value"];
        }
        return cell;
    }
    
    detailsFirstTableViewCell *cell=(detailsFirstTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"detailsFirstTableViewCell"];
    
    if(cell == nil){
        
        cell=[[detailsFirstTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"detailsFirstTableViewCell"];
        
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.nameLabel.text=@"正在加载。。。";
    
    if (detailsDict2) {
        
        cell.priceLabel.text=[detailsDict2 objectForKey:@"price"];
        cell.nameLabel.text=[detailsDict2 objectForKey:@"shop_name"];
        
        NSString *str = [NSString stringWithFormat:@"库存：%@", [detailsDict2 objectForKey:@"inventory"]];
        cell.kcLabel.text = str;
    }
    if (detailsArr3) {
        
        [cell updateInfo:detailsArr3];
        
    }
    return cell;
    
}

//选中Cell响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
    
}


#pragma mark - click
-(void)rightNavItemClick{
    
    NSLog(@"分享");
    
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    _shadow.hidden = NO;
    _shadow.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT);
    [UIView commitAnimations];
}

-(void)gwdButtonClick{
    
    NSLog(@"加入购物袋");
    
    // 登陆判断
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:Is_Login
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       [self addShop];
                                       [hud removeFromSuperview];
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       loginViewController *loginVc =[[loginViewController alloc]init];
                                       [self.navigationController presentViewController:loginVc animated:YES completion:^{}];
                                   }
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

- (void)addShop{

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    NSString *uidStr=[PublicFunction getConfigWithKey:@"uid"];
    NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
    [productParams setObject: self.myStr forKey:@"shop_id"];//self.myStr
    [productParams setValue:uidStr forKey:@"member_id"];
    [productParams setValue:tokenStr forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:Cart_Add
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:@"yes",@"select", nil];
                                       NSNotification *notification =[NSNotification notificationWithName:@"cart_num" object:nil userInfo:dict];
                                       [[NSNotificationCenter defaultCenter] postNotification:notification];
                                       
                                       sleep(1);
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:@"加入购物袋成功" duration:5.0f];
                                       [detailsTableView reloadData];
                                       
                                   }
                                   else if ([[returnData objectForKey:@"code"]intValue]==-1){
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

-(void)hButtonClick{
    
    NSLog(@"购物袋");
    
    [self.tabBarController setSelectedIndex:2];
    [self.navigationController popToRootViewControllerAnimated:NO];
    
}

-(void)xButtonClick{
    
    NSLog(@"收藏");
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    NSString *uidStr=[PublicFunction getConfigWithKey:@"uid"];
    NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
    [productParams setObject: self.myStr forKey:@"shop_id"];//self.myStr
    [productParams setValue:uidStr forKey:@"member_id"];
    [productParams setValue:tokenStr forKey:@"token"];
    [productParams setValue:@"1" forKey:@"type"];
    
    [[MNetworkHandler sharedInstance]conURL:Collection
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:10.0f];
                                       xImageView.image = [UIImage imageNamed:@"collection_sel"];
                                       
                                       [hud removeFromSuperview];
                                   }
                                   else if ([[returnData objectForKey:@"code"]intValue]==-1){
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                                   else if ([[returnData objectForKey:@"code"]intValue]== 2){
                                       
                                       [hud removeFromSuperview];
                                   
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:10.0f];
                                       xImageView.image=[UIImage imageNamed:@"collection_nor"];
                                   }
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

- (void)loadMoreTopic{

    [detailsTableView.mj_footer endRefreshing];
    
    picHeightArray = [[NSMutableArray alloc]init];
    footViewHeight = 0;
    
    for (int i = 0 ; i < [picArr count]; i ++) {
        
        NSString *urlGood = picArr[i][@"path"];
        NSString *urlN = manage(AP_HOST, urlGood);
        NSString *strUrl = [urlN stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
        
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:strUrl]];
        UIImage *image = [UIImage imageWithData:data];
        size = image.size;
        
        footViewHeight += size.height/(size.width/scrWidth());
        [picHeightArray insertObject:[NSString stringWithFormat:@"%f",size.height/(size.width/scrWidth())] atIndex:i];
    }
    
    if (picArr.count > 0) {
        
        hidden = YES;
    }
    
    [detailsTableView reloadData];
    
}

@end
